package com.wx.disparent.mallparent.controller;

import com.wx.disparent.mallparent.entity.MerchantEntity;
import com.wx.disparent.mallparent.entity.ProductEntity;
import com.wx.disparent.mallparent.service.MerchantService;
import com.wx.disparent.mallparent.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author 刘杰龙
 * @version 1.0
 * @description 共用控制器
 * @date 2019/12/16 16:16
 */
@RestController
@RequestMapping("/com")
public class CommonController {
    /**
     * 商家服务
     */
    @Autowired
    private MerchantService ms;
    /**
     * 商品服务
     */
    @Autowired
    private ProductService ps;

    /**
     * 访问返回后台主页面
     */
    @RequestMapping("/index")
    public ModelAndView index() {
        ModelAndView model = new ModelAndView();
        model.setViewName("index");
        return model;
    }

    @RequestMapping("/out")
    public ModelAndView out() {
        ModelAndView model = new ModelAndView();
        model.setViewName("out");
        return model;
    }

    @RequestMapping("/outLogin")
    public ModelAndView outLogin() {
        ModelAndView model = new ModelAndView();
        model.setViewName("outLogin");
        return model;
    }

    /**
     * 错误请求处理
     */
    @RequestMapping("/404")
    public void error(HttpSession session, HttpServletResponse response) {
        Object userEntity = session.getAttribute("userEntity");
        try {
            if (userEntity == null) {
                response.sendRedirect("/com/outLogin");
            } else {
                response.sendRedirect("/com/out");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 后台退出
     */
    @RequestMapping("/logout/admin")
    public ModelAndView logoutAdmin(HttpSession session) {
        session.removeAttribute("userEntity");
        ModelAndView model = new ModelAndView();
        model.setViewName("web/pc/loginPage");
        return model;
    }

    /**
     * 回后前台
     */
    @RequestMapping("/logout/front")
    public ModelAndView logoutFront(HttpSession session) {
        session.removeAttribute("userEntity");
        ModelAndView model = new ModelAndView();
        model.setViewName("web/login");
        return model;
    }

    /**
     * 验证商品信息
     */
    @RequestMapping("/verification/product")
    public String verification(ProductEntity productEntity) {
        StringBuffer sb = new StringBuffer();

        String name = productEntity.getName();
        BigDecimal price = productEntity.getPrice();
        BigDecimal merRatio = productEntity.getMerRatio();
        BigDecimal referRatio = productEntity.getReferRatio();
        String image = productEntity.getImage();
        String introduce = productEntity.getIntroduce();

        //商品名称
        if (judgeNull(name)) {
            if (name.length() > 50) {
                sb.append("{'nam':'商品名称过长！',");
            } else {
                sb.append("{'nam':'√',");
            }
        } else {
            sb.append("{'nam':'商品名称为空！',");
        }
        //商品价格
        if (price != null) {
            if (judgeMinusOrZero(price)) {
                if (price.compareTo(new BigDecimal(Math.pow(10, 16))) == 1) {
                    sb.append("pri:'商品价格过大！',");
                } else {
                    sb.append("pri:'√',");
                }
            } else {
                sb.append("pri:'商品价格为负或零！',");
            }
        } else {
            sb.append("pri:'未输入商品价格！',");
        }

        /*//供应商分配比例
        if (merRatio != null) {
            if (judgeMinusOrZero(merRatio)) {
                if (merRatio.compareTo(new BigDecimal(Math.pow(10, 16))) == 1) {
                    sb.append("mer:'供应商分配比例过大！',");
                } else {
                    sb.append("mer:'√',");
                }
            } else {
                sb.append("mer:'供应商分配比例为负或零！',");
            }
        } else {
            sb.append("mer:'未输入供应商分配比例！',");
        }

        //推荐人分配比例
        if (referRatio != null) {
            if (judgeMinusOrZero(referRatio)) {
                if (referRatio.compareTo(new BigDecimal(Math.pow(10, 16))) == 1) {
                    sb.append("refer:'推荐人分配比例过大！',");
                } else {
                    sb.append("refer:'√',");
                }
            } else {
                sb.append("refer:'推荐人分配比例为负或零！',");
            }
        } else {
            sb.append("refer:'未输入推荐人分配比例！',");
        }*/

        //商品图片
        if (judgeNull(image)) {
            if (image.length() > 140) {
                sb.append("imga:'商品图片不可超过5个！',");
            } else {
                sb.append("imga:'√',");
            }
        } else {
            sb.append("imga:'商品图片未上传！',");
        }
        //商品介绍
        if (judgeNull(introduce)) {
            sb.append("intro:'√'}");
        } else {
            sb.append("intro:'商品介绍为空！'}");
        }

        return sb.toString();
    }

    /**
     * 验证商家信息
     */
    @RequestMapping("/verification/merchant")
    public String verification(MerchantEntity merchantEntity) {
        StringBuffer sb = new StringBuffer();

        String name = merchantEntity.getName();
        String phone = merchantEntity.getPhone();
        String bank = merchantEntity.getBankCardNum();
        String bankName = merchantEntity.getBankCardName();
        String alipay = merchantEntity.getAlipayNum();
        String wechat = merchantEntity.getWechatNum();
        Integer id = merchantEntity.getId();

        //商家姓名
        if (judgeNull(name)) {
            if (name.length() > 100) {
                sb.append("{'nam':'商家姓名过长！',");
            } else {
                sb.append("{'nam':'√',");
            }
        } else {
            sb.append("{'nam':'商家姓名为空！',");
        }
        //联系电话
        if (judgeNull(phone)) {
            if (!judgePhone(phone)) {
                sb.append("'pho':'输入的联系电话格式不正确！',");
            } else {
                if (ms.findByPhone(phone) != null && id == null) {
                    sb.append("'pho':'联系电话已存在',");
                } else {
                    sb.append("'pho':'√',");
                }
            }
        } else {
            sb.append("'pho':'联系电话为空！',");
        }
        //商家银行卡账号
        if (judgeNull(bank)) {
            if (bank.length() > 30) {
                sb.append("'bank':'银行卡账号过长！',");
            } else if (!judgeNumber(bank)){
                sb.append("'bank':'银行卡账号非纯数字！',");
            } else {
                sb.append("'bank':'√',");
            }
        } else {
            sb.append("'bank':'银行卡账号为空！',");
        }
        //商家银行卡持卡人姓名
        if (judgeNull(bankName)) {
            if (bankName.length() > 30) {
                sb.append("'bana':'银行卡持卡人姓名过长！',");
            } else {
                sb.append("'bana':'√',");
            }
        } else {
            sb.append("'bana':'银行卡持卡人姓名为空！',");
        }
       /* //商家银行卡开户行
        if (judgeNull(bankType)) {
            if (bankType.length() > 30) {
                sb.append("'baty':'银行卡开户行过长！',");
            } else {
                sb.append("'baty':'√',");
            }
        } else {
            sb.append("'baty':'银行卡开户行为空！',");
        }*/
        //商家支付宝账号
        if (judgeNull(alipay)) {
            if (alipay.length() > 30) {
                sb.append("'ali':'支付宝账号过长！',");
           /* } else if (!judgeNumber(alipay)){
                sb.append("'ali':'支付宝账号非纯数字！',");*/
            } else {
                sb.append("'ali':'√',");
            }
        } else {
            sb.append("'ali':'支付宝账号为空！',");
        }
        //商家微信账号
        if (judgeNull(wechat)) {
            if (wechat.length() > 30) {
                sb.append("'wec':'微信账号过长！'}");
            /*} else if (!judgeNumber(wechat)){
                sb.append("'wec':'微信账号非纯数字！',");*/
            } else {
                sb.append("'wec':'√'}");
            }
        } else {
            sb.append("'wec':'微信账号为空！'}");
        }

        return sb.toString();
    }

    /**
     * 字符串判空
     */
    private boolean judgeNull(String s) {
        if (s != null && !s.equals("")) {
            return true;
        }
        return false;
    }

    /**
     * 数字判正
     */
    private boolean judgeMinusOrZero(BigDecimal b) {
        if (b.compareTo(BigDecimal.ZERO) == 1) {
            return true;
        }
        return false;
    }

    /**
     * 字符纯数字串判
     */
    private boolean judgeNumber(String s) {
        Pattern pattern = Pattern.compile("^[1-9][0-9]*$");
        Matcher isNum = pattern.matcher(s);
        return isNum.matches();
    }

    /**
     * 电话号码验证
     */
    private boolean judgePhone(String s) {
        Pattern p = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
        Matcher m = p.matcher(s);
        boolean matches = m.matches();
        return matches;
    }
}
